package com.educate.common.web;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.util.List;

public class RequestUtils {

    public static PageRequest parsePageRequest(Integer page, Integer size, List<String> sortProps, Boolean sortAsc) {
        if (page == null) page = 1;
        if (size == null) size = Integer.MAX_VALUE;
        page = page - 1;
        if (page < 0) page = 0;
        if (sortProps == null || sortProps.isEmpty()) return new PageRequest(page, size);
        if (sortAsc == null) sortAsc = false;
        return new PageRequest(page, size, new Sort(sortAsc ? Sort.Direction.ASC : Sort.Direction.DESC, sortProps));
    }
}
